/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.EnquiryDAO;
import Models.Enquiry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author phuong
 */
@ManagedBean
@RequestScoped
public class EnquiryController {

    List<Enquiry> list = new ArrayList();

    public List<Enquiry> getList() throws Exception {
        return list = EnquiryDAO.getListEnquiry();
    }

    public void setList(List<Enquiry> list) {
        this.list = list;
    }

    public void sendEnquiry(Enquiry en) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        if (session.getAttribute("selectedCompany") != null) {
            en.setCompanyID(Integer.parseInt(session.getAttribute("selectedCompany").toString()));
            session.setAttribute("selectedCompany", null);
        } else {
            session.setAttribute("selectedCompany", null);
        }
        if (session.getAttribute("selectedDriver") != null) {
            en.setDriverID(Integer.parseInt(session.getAttribute("selectedDriver").toString()));
            session.setAttribute("selectedDriver", null);
        } else {
            session.setAttribute("selectedDriver", null);
        }
        EnquiryDAO.addEnquiry(en);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Add has been successed !"));
    }

    public void enquiryCompany(int selectedID) {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            session.setAttribute("selectedCompany", selectedID);
            FacesContext.getCurrentInstance().getExternalContext().redirect("EnquiryCompany.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EnquiryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void enquiryDriver(int selectedID) {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            session.setAttribute("selectedDriver", selectedID);
            FacesContext.getCurrentInstance().getExternalContext().redirect("EnquiryDriver.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EnquiryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public void deleteEnquery() throws Exception {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        EnquiryDAO.deleteEnquiryByID(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Enquery has been deleted !"));
    }

    /**
     * Creates a new instance of EnquiryController
     */
    public EnquiryController() {
    }
}
